package oa.hleast.xswl.controller.admin;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import oa.hleast.common.enums.QueryModeEnum;
import oa.hleast.common.result.Result;
import oa.hleast.xswl.pojo.domain.sport.WlVenuePlace;
import oa.hleast.xswl.service.WlVenuePlaceService;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;


@Api(tags = "【系统管理】体育场地表;关联:hasMany=VenuePlacePicture,picture_id")
@RestController("AdminWlVenuePlaceController")
@RequestMapping("/api.admin/v1/wlVenuePlaces")
@Slf4j
@AllArgsConstructor
public class WlVenuePlaceController {

    private final WlVenuePlaceService wlVenuePlaceService;

    @ApiOperation(value = "列表分页", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "queryMode", value = "查询模式", paramType = "query", dataTypeClass = QueryModeEnum.class, dataType = "QueryModeEnum"),
            @ApiImplicitParam(name = "page", value = "页码", paramType = "query", dataTypeClass = Long.class, dataType = "Long"),
            @ApiImplicitParam(name = "limit", value = "每页数量", paramType = "query", dataTypeClass = Long.class, dataType = "Long")
    })
    @GetMapping
    public Result list(
            String queryMode,
            Integer page,
            Integer limit) {
        QueryModeEnum queryModeEnum = QueryModeEnum.getValue(queryMode);
        switch (queryModeEnum) {
            default:
                LambdaQueryWrapper<WlVenuePlace> queryWrapper = new LambdaQueryWrapper<WlVenuePlace>()
                        .orderByDesc(WlVenuePlace::getGmtModified)
                        .orderByDesc(WlVenuePlace::getGmtCreate);

                Page<WlVenuePlace> result = wlVenuePlaceService.page(new Page<>(page, limit), queryWrapper);
                return Result.success(result.getRecords(), result.getTotal());
        }
    }


    @ApiOperation(value = "新增", httpMethod = "POST")
    @PostMapping
    public Result add(@RequestBody WlVenuePlace body) {
        boolean status = wlVenuePlaceService.save(body);
        return Result.judge(status);
    }

    @ApiOperation(value = "详情", httpMethod = "GET")
    @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "path", dataTypeClass = Long.class, dataType = "Long")
    @GetMapping("/{id}")
    public Result detail(@PathVariable Long id) {
        return Result.success(wlVenuePlaceService.getById(id));
    }

    @ApiOperation(value = "修改", httpMethod = "PUT")
    @PutMapping
    public Result update(
            @RequestBody WlVenuePlace body) {
        body.setGmtModified(new Date());
        boolean status = wlVenuePlaceService.updateById(body);
        return Result.judge(status);
    }

    @ApiOperation(value = "删除标签", httpMethod = "DELETE")
    @ApiImplicitParam(name = "ids", value = "id集合", required = true, paramType = "query", dataTypeClass = String.class, dataType = "String")
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable("ids") String ids) {
        boolean status = wlVenuePlaceService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.judge(status);
    }
}
